package cn.sjtu.netty.bytebuffer;


import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;

/**
 * 测试ByteBuffer的入门程序
 */
@Slf4j
public class TestByteBuffer {

    /**
     * 读取电脑上的文件并打印出来
     * @param args 虚拟机参数
     */
    public static void main(String[] args) {
        // 1. 获取FileChannel的方式
        try (FileChannel channel = new FileInputStream("test.txt").getChannel()) {

            // 2. 获取ByteBuffer
            ByteBuffer buffer = ByteBuffer.allocate(10);

            int len = 0;
            while ((len = channel.read(buffer)) != -1) {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    log.info("读取到字符为：{}", (char)buffer.get());
                }
                buffer.clear();
            }
        } catch (IOException e) {
        }
    }
}
